import java.util.HashSet;
import java.util.Set;
class Solution {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while( !set.contains(n) && n != 1){
            set.add(n);
            n = calculation(n);
        }
        return n == 1;

    }

    public static int calculation(int n){
        int sum = 0;
        while (n > 0){
            int i = n % 10;
            sum += i*i;
            n = n/10;

        }
        return sum;
    }
}